/**
 * 
 */
package com.sebscape.sebcms.setup.domain;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.appengine.api.datastore.Text;
import com.sebscape.sebcms.persistence.domain.IPersistedObject;
import com.sebscape.sebcms.persistence.domain.IUpdateable;
import com.sebscape.sebcms.tools.Utils;

/**
 * @author Stephen
 * 
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Unique(name = "PAGE_UNIQUE_IDX", members = { "customUrl" })
public class SetupConfiguration implements Serializable, IPersistedObject, IUpdateable<SetupConfiguration>{
	
	private static final long serialVersionUID = 5663300248131459939L;
	private static Log log = LogFactory.getLog(SetupConfiguration.class);
	
	/** When the configuration is stored in a scope (session, application, etc.) it should be refernced
	 * using this name.
	 */
	public static final String SCOPED_VARIABLE_NAME = "sebcms_site_configuration";

	/**
	 * 
	 */
	public SetupConfiguration() {
		super();
	}

	/**
	 * @param name
	 * @param description
	 * @param contactInfo
	 * @param emailServer
	 * @param emailAdminFrom
	 * @param emailSmtpUsername
	 * @param emailSmtpPassword
	 * @param emailAdminSubjectPrefix
	 * @param emailDisclaimerContent
	 * @param picasaDefaultAlbumName
	 * @param picasaServiceUsername
	 * @param picasaServicePassword
	 */
	public SetupConfiguration(String name, String description,
								String contactInfo, String emailServer, 
								String emailAdminFrom, String emailSmtpUsername, 
								String emailSmtpPassword, String emailAdminSubjectPrefix, 
								String emailDisclaimerContent, String picasaDefaultAlbumName, 
								String picasaServiceUsername,  String picasaServicePassword,
								String emailTemplateWelcomeUser, String emailTemplateLostPassword, 
								String emailTemplateNotice) {
		super();
		this.name = name;
		this.description = description;
		this.contactInfo = contactInfo;
		this.emailServer = emailServer;
		this.emailAdminFrom = emailAdminFrom;
		this.emailSmtpUsername = emailSmtpUsername;
		this.emailSmtpPassword = emailSmtpPassword;
		this.emailAdminSubjectPrefix = emailAdminSubjectPrefix;
		this.emailDisclaimerContent = emailDisclaimerContent;
		this.picasaDefaultAlbumName = picasaDefaultAlbumName;
		this.picasaServiceUsername = picasaServiceUsername;
		this.picasaServicePassword = picasaServicePassword;
		this.emailTemplateWelcomeUser = new Text( emailTemplateWelcomeUser );
		this.emailTemplateLostPassword = new Text( emailTemplateLostPassword );
		this.emailTemplateNotice = new Text( emailTemplateNotice );

	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	// /////////////////
	// general config
	// ////////////////
	@Persistent
	private String name;

	@Persistent
	private String description;

	@Persistent
	private String contactInfo;
	
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the contactInfo
	 */
	public String getContactInfo() {
		return contactInfo;
	}

	/**
	 * @param contactInfo the contactInfo to set
	 */
	public void setContactInfo(String contactInfo) {
		this.contactInfo = contactInfo;
	}

	// /////////////////
	// email config
	// ////////////////
	@Persistent
	private String emailServer;

	@Persistent
	private String emailAdminFrom;

	@Persistent
	private String emailSmtpUsername;

	@Persistent
	private String emailSmtpPassword;

	@Persistent
	private String emailAdminSubjectPrefix;

	@Persistent
	private String emailDisclaimerContent;
	
	@Persistent(defaultFetchGroup="true")
	private Text emailTemplateWelcomeUser;
	
	@Persistent(defaultFetchGroup="true")
	private Text emailTemplateLostPassword;
	
	@Persistent(defaultFetchGroup="true")
	private Text emailTemplateNotice;
	
	/**
	 * @return the emailServer
	 */
	public String getEmailServer() {
		return emailServer;
	}

	/**
	 * @param emailServer the emailServer to set
	 */
	public void setEmailServer(String emailServer) {
		this.emailServer = emailServer;
	}

	/**
	 * @return the emailAdminFrom
	 */
	public String getEmailAdminFrom() {
		return emailAdminFrom;
	}

	/**
	 * @param emailAdminFrom the emailAdminFrom to set
	 */
	public void setEmailAdminFrom(String emailAdminFrom) {
		this.emailAdminFrom = emailAdminFrom;
	}

	/**
	 * @return the emailSmtpUsername
	 */
	public String getEmailSmtpUsername() {
		return emailSmtpUsername;
	}

	/**
	 * @param emailSmtpUsername the emailSmtpUsername to set
	 */
	public void setEmailSmtpUsername(String emailSmtpUsername) {
		this.emailSmtpUsername = emailSmtpUsername;
	}

	/**
	 * @return the emailSmtpPassword
	 */
	public String getEmailSmtpPassword() {
		return emailSmtpPassword;
	}

	/**
	 * @param emailSmtpPassword the emailSmtpPassword to set
	 */
	public void setEmailSmtpPassword(String emailSmtpPassword) {
		this.emailSmtpPassword = emailSmtpPassword;
	}

	/**
	 * @return the emailAdminSubjectPrefix
	 */
	public String getEmailAdminSubjectPrefix() {
		return emailAdminSubjectPrefix;
	}

	/**
	 * @param emailAdminSubjectPrefix the emailAdminSubjectPrefix to set
	 */
	public void setEmailAdminSubjectPrefix(String emailAdminSubjectPrefix) {
		this.emailAdminSubjectPrefix = emailAdminSubjectPrefix;
	}

	/**
	 * @return the emailDisclaimerContent
	 */
	public String getEmailDisclaimerContent() {
		return emailDisclaimerContent;
	}

	/**
	 * @param emailDisclaimerContent the emailDisclaimerContent to set
	 */
	public void setEmailDisclaimerContent(String emailDisclaimerContent) {
		this.emailDisclaimerContent = emailDisclaimerContent;
	}

	
	/**
	 * @return the emailTemplateWelcomeUser
	 */
	public String getEmailTemplateWelcomeUser() {
		return emailTemplateWelcomeUser.getValue();
	}

	/**
	 * @param emailTemplateWelcomeUser the emailTemplateWelcomeUser to set
	 */
	public void setEmailTemplateWelcomeUser(Text emailTemplateWelcomeUser) {
		log.debug( "called setEmailTemplateWelcomeUser with TEXT: " + emailTemplateWelcomeUser.getValue() );
		this.emailTemplateWelcomeUser = emailTemplateWelcomeUser;
	}

	/**
	 * @param emailTemplateWelcomeUser the emailTemplateWelcomeUser to set
	 */
	public void setEmailTemplateWelcomeUser(String emailTemplateWelcomeUser) {
		log.debug( "called setEmailTemplateWelcomeUser with String: " + emailTemplateWelcomeUser );
		this.setEmailTemplateWelcomeUser( new Text( emailTemplateWelcomeUser ) );
	}
	
	/**
	 * @return the emailTemplateLostPassword
	 */
	public String getEmailTemplateLostPassword() {
		return emailTemplateLostPassword.getValue();
	}

	/**
	 * @param emailTemplateLostPassword the emailTemplateLostPassword to set
	 */
	public void setEmailTemplateLostPassword(Text emailTemplateLostPassword) {
		this.emailTemplateLostPassword = emailTemplateLostPassword;
	}

	/**
	 * @param emailTemplateLostPassword the emailTemplateLostPassword to set
	 */
	public void setEmailTemplateLostPassword( String emailTemplateLostPassword) {
		this.setEmailTemplateLostPassword( new Text( emailTemplateLostPassword ) );
	}
	
	/**
	 * @return the emailTemplateNotice
	 */
	public Text getEmailTemplateNotice() {
		return emailTemplateNotice;
	}

	/**
	 * @param emailTemplateNotice the emailTemplateNotice to set
	 */
	public void setEmailTemplateNotice(Text emailTemplateNotice) {
		this.emailTemplateNotice = emailTemplateNotice;
	}

	/**
	 * @param emailTemplateNotice the emailTemplateNotice to set
	 */
	public void setEmailTemplateNotice(String emailTemplateNotice) {
		this.setEmailTemplateNotice( new Text( emailTemplateNotice ) );
	}
	
	/////////////////////
	// Slideshow config
	////////////////////

	@Persistent
	private String picasaDefaultAlbumName;

	@Persistent
	private String picasaServiceUsername;

	@Persistent
	private String picasaServicePassword;


	/**
	 * @return the picasaDefaultAlbumName
	 */
	public String getPicasaDefaultAlbumName() {
		return picasaDefaultAlbumName;
	}

	/**
	 * @param picasaDefaultAlbumName the picasaDefaultAlbumName to set
	 */
	public void setPicasaDefaultAlbumName(String picasaDefaultAlbumName) {
		this.picasaDefaultAlbumName = picasaDefaultAlbumName;
	}

	/**
	 * @return the picasaServiceUsername
	 */
	public String getPicasaServiceUsername() {
		return picasaServiceUsername;
	}

	/**
	 * @param picasaServiceUsername the picasaServiceUsername to set
	 */
	public void setPicasaServiceUsername(String picasaServiceUsername) {
		this.picasaServiceUsername = picasaServiceUsername;
	}

	/**
	 * @return the picasaServicePassword
	 */
	public String getPicasaServicePassword() {
		return picasaServicePassword;
	}

	/**
	 * @param picasaServicePassword the picasaServicePassword to set
	 */
	public void setPicasaServicePassword(String picasaServicePassword) {
		this.picasaServicePassword = picasaServicePassword;
	}

	@Override
	public void update(SetupConfiguration newValues) {
		this.name = newValues.name;
		this.contactInfo = newValues.contactInfo;
		this.description = newValues.description;
		this.emailAdminFrom = newValues.emailAdminFrom;
		this.emailAdminSubjectPrefix = newValues.emailAdminSubjectPrefix;
		this.emailDisclaimerContent = newValues.emailDisclaimerContent;
		this.emailServer = newValues.emailServer;
		this.emailSmtpPassword = newValues.emailSmtpPassword;
		this.emailSmtpUsername = newValues.emailSmtpUsername;
		this.picasaDefaultAlbumName = newValues.picasaDefaultAlbumName;
		if ( !Utils.isEmpty( newValues.picasaServicePassword ) ) {
			this.picasaServicePassword = newValues.picasaServicePassword;
		}
		this.picasaServiceUsername = newValues.picasaServiceUsername;
		this.emailTemplateLostPassword = newValues.emailTemplateLostPassword;
		this.emailTemplateNotice = newValues.emailTemplateNotice;
		this.emailTemplateWelcomeUser = newValues.emailTemplateWelcomeUser;
	}	
}
